package com.topnav.mc.admin.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.bean.OrganizationBean;
import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.admin.entity.param.OrganizationParam;
import com.topnav.mc.base.mc.McMapper;
import org.apache.ibatis.annotations.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.topnav.mc.admin.entity.Organization;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
//public interface OrganizationMapper extends BaseMapper<Organization>{
public interface OrganizationMapper extends McMapper<Organization, OrganizationBean, OrganizationParam> {
    String commSql = " SELECT o.*, " +
            " p.code AS parentCode, p.name AS parentName, p.short_name AS parentShortName, " +
            " p1.hasChildrens " +
            " FROM mc_organization o " +
            " LEFT JOIN mc_organization p ON o.parent_id = p.id " +
            " LEFT JOIN (SELECT parent_id,1 AS hasChildrens  FROM mc_organization p1 GROUP BY parent_id) p1 ON o.id = p1.parent_id " +
            " ${ew.customSqlSegment}";
    @Select(commSql)
    OrganizationBean selectInfoOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    List<OrganizationBean> selectInfoList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    Page<OrganizationBean> selectInfoPage(Page<OrganizationParam> page, @Param(Constants.WRAPPER) Wrapper queryWrapper);


    @Select("SELECT * FROM mc_organization ORDER BY ORDER BY path")
    List<Organization> findAll();
    /**
     * 取得该用户的所有单位
     * @param userId
     * @return
     */
    List<Organization> fingAllOrgByUser(@Param("userId") Long userId);


}
